/** ======================================================= **
 * 	MemberMaintenancePanel.js
 ** ======================================================= **/
	MemberMaintenancePanel = Ext.extend(Ext.grid.GridPanel,{
		cm: null,
		store: null,
		condition: null,
		roleUserSettingWin: null,
 		constructor:function(config){
 			Ext.apply(this, config);
 			
 			this.condition = {projectId: 0};
 			
 			this.store = new Ext.data.Store({
 				baseParams: this.condition,
				proxy: new Ext.data.HttpProxy({
					method: 'POST',
					url: '/pdm/role/getUserRoleList.htm'
				}),
				reader: new Ext.data.JsonReader({
					totalProperty: 'totalProperty',
					root: 'root',
					id: 'role_id'
				},[
					{name: 'role_id'},
					{name: 'roleName'},
					{name: 'userName'},
					{name: 'userId'},
					{name: 'userMail'},
					{name: 'userPhone'},
					{name: 'userExt'}
				])
			});
			
			this.store.load();
						
			this.cm = new Ext.grid.ColumnModel([
				new Ext.grid.RowNumberer(),
				{
					id: 'roleName', 
					header: "职务名称",
					dataIndex: 'roleName',
					sortable: true,
					anchor:'17%',
					renderer: function(v,m){
						m.css = 'x-grid-back-light-color';
						return v;
					}
				},{
					header: "姓名",
					dataIndex: 'userName',
					align: 'left',
					sortable: true,
					anchor: '18%'
				},{
					header: "工号",
					dataIndex: 'userId',
					hidden: false,
					sortable:true,
					anchor:'10%'
				},{
					header: "邮箱",
					dataIndex: 'userMail',
					hidden: false,
					sortable:true,
					anchor:'30%'
				},{
					header: "分机",
					dataIndex: 'userExt',
					hidden: false,
					sortable: true,
					anchor:'10%'
				},{
					header: "手机",
					dataIndex: 'userPhone',
					hidden: false,
					sortable:true,
					anchor:'15%'
				}
			]);
			
			MemberMaintenancePanel.superclass.constructor.call(this, {
				loadMask: {msg: "人员载入中..."},
				region: 'center',
				border: false,
				cls: 'x-rb',
				ds: this.store,
				sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
				cm: this.cm,
				trackMouseOver: false,
				stripeRows: true,
				columnLines: true,
				height: 300,
				width: 900,
				viewConfig: {
					forceFit: true,
					enableRowBody: true,
					showPreview: true
				},
				tbar:[{
					id: 'btn_edit_role',
					text: "人员设置",
					tooltip: "设置当前项目中的职务负责人",
					iconCls: 'cls_edit_role',
					scope: this,
					handler: function(){
						var record = this.getSelectionModel().getSelected();
						
						if(!record){
							Ext.Msg.alert("温馨提示：","请先选中需要设置的岗位！");
						}else{
							var roleId = record.get("role_id");
							var roleName = record.get("roleName");
							
							var projectTree = Ext.getCmp("combo_project_tree").comboBoxGrid;
							var node = projectTree.getSelectionModel().getSelectedNode();
							var projectId = node.id;
							
							if(!this.roleUserSettingWin){
								this.roleUserSettingWin = new RoleUserSettingWindow({id: "win_RoleUserSetting"});
							}
							this.roleUserSettingWin.reDoLayout();
							this.roleUserSettingWin.setTitle("对职务["+roleName+"] 设置负责人员");
							
							var members = getRoleUserList(roleId, projectId);
							
							this.roleUserSettingWin.roleUserSettingPanel.roleUserSelect.setReCosigners(members);
							this.roleUserSettingWin.roleUserSettingPanel.roleUserSelect.load();
							
							this.roleUserSettingWin.show();
							
						}
					}
				},"-",{
					xtype: 'tbspacer',
					width: 20
				},"项目：",new ComboBoxMyProject({id: "combo_project_tree"})
				/*{
					xtype: 'combo',
					width: 80,
					id: 'combo_my_project',
					name: 'combo_my_project',
					forceSelection: true,
					store: new Ext.data.JsonStore({
						url:'/pdm/category/getMyOwnProject.htm',
						totalProperty: 'totalProperty',
						root: 'root',
						fields: ['key','name']
					}),
					valueField: 'key',
					displayField: 'name',
					emptyText: "请选择项目...",
					loadingText: "正在加载所有项目",
					typeAhead: true,
					mode: 'remote',
					//readOnly: true,
					editable: false,
					triggerAction: 'all',
					selectOnFocus: true,
					listeners: {
					    select: function(combo, record, index){
							var projectId = record.data.key;
							var gpProjectMember = Ext.getCmp("gpMemberMaintenance");
							if(Ext.isEmpty(gpProjectMember)) Ext.Msg.alert("糟糕，系统出问题啦！");
							else{
								gpProjectMember.reload(projectId);
							}
						}
					}
				},"版本：",{
					xtype: 'combo',
					width: 50,
					id: 'combo_project_version',
					name: 'combo_project_version',
					forceSelection: true,
					store: new Ext.data.JsonStore({
						url:'/pdm/category/getProjectVersion.htm',
						totalProperty: 'totalProperty',
						root: 'root',
						fields: ['key','name']
					}),
					valueField: 'key',
					displayField: 'name',
					emptyText: "请选择项目版本...",
					loadingText: "正在加载项目版本...",
					typeAhead: true,
					mode: 'remote',
					editable: false,
					triggerAction: 'all',
					selectOnFocus: true,
					listeners: {
					    select: function(combo, record, index){
							var versionId = record.data.key;
							alert(versionId);
						}
					}
				},"阶段：",{
					xtype: 'combo',
					width: 100,
					id: 'combo_project_stage',
					name: 'combo_project_stage',
					forceSelection: true,
					store: new Ext.data.JsonStore({
						url:'/pdm/category/getProjectStage.htm',
						totalProperty: 'totalProperty',
						root: 'root',
						fields: ['key','name']
					}),
					valueField: 'key',
					displayField: 'name',
					emptyText: "请选择阶段...",
					loadingText: "正在加载阶段...",
					typeAhead: true,
					mode: 'remote',
					editable: false,
					triggerAction: 'all',
					selectOnFocus: true,
					listeners: {
					    select: function(combo, record, index){
							var versionId = record.data.key;
							alert(versionId);
						}
					}
				}*/
				],
				bbar: new Ext.PagingToolbar({
					pageSize: 50,
					store: this.store,
					displayInfo: true,
					displayMsg: "显示第 {0}~{1} 条，共 {2} 条记录",
					emptyMsg: "没有记录",
					refreshText: "刷新",
					beforePageText: "第",
					afterPageText: "页，共 {0} 页",
					firstText: "第一页",
					prevText: "前一页",
					nextText: "后一页",
					lastText: "最后一页"
				})
			});
			this.getSelectionModel().on('rowselect', function(sm, rowIdx, r){
				document.getElementById("_selected_role_id").value = r.get("role_id");
			})
		},
		load: function(){
			this.condition.projectId = 0;
			this.store.load();
		},
		reload: function(c){
			this.condition.projectId = c;
			this.store.load();
		}
	});

	
	
/****************************************************
 * @class AddUserRoleWindow
 * @extends Ext.Window
 ***************************************************/
	AddUserRoleWindow = Ext.extend(Ext.Window,{
		addUserRolePanel: null,
		roleUserPanel: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.roleUserPanel = new AddRoleUserPanel({id:"addRoleUserPanel", reCosignerList:""});
			this.addUserRolePanel = new AddUserRolePanel({id:"addRoleUserPanel", roleUserPanel: this.roleUserPanel});
			
			AddUserRoleWindow.superclass.constructor.call(this, {
				layout: 'fit',
				width: 480,
				height: 360,
				modal: true,
				iconCls: 'cls_role_add',
				closeAction: 'hide',
				plain: true,
				buttonAlign: 'center',
				resizable: false,
				items: this.addUserRolePanel,
				buttons: [{
					text: "保存",
					disabled: false,
					scope: this,
					handler: function(){
						var addUserRolePanel = Ext.getCmp("addUserRolePanel");
						if (addUserRolePanel.getForm().isValid()) {
							
							addUserRolePanel.form.setValues({
								"userList": document.getElementById("_collectReCosign").value
							});
							
							addUserRolePanel.form.submit({
								url : '/pdm/role/saveUserRoles.htm',
								success : function(form, action)
								{
									/*
									var msg = action.response.responseText;
									var obj = Ext.decode( msg );
									Ext.Msg.alert('温馨提示：', obj.message);
									*/
									var addUserRoleWin = Ext.getCmp("win_AddUserRole");
									addUserRoleWin.hide();
									
									var gpMemberMaintenance = Ext.getCmp("gpMemberMaintenance");
									gpMemberMaintenance.load();
									
								},
								failure : function(form, action) 
								{
									Ext.Msg.alert('温馨提示：', '信息保存失败!');
								},
								waitMsg : '正在保存数据，稍后...'
							});
						}else{
							Ext.Msg.alert('信息', '请确认红色方框里内容是否完整或正确!');
						}
					}
				},{
					text: "取消",
					scope: this,
					handler: function(){
						this.hide();
					}
				}]
			});
		},
		reset: function()
		{
			this.addUserRolePanel.form.reset();
			this.roleUserPanel.load("");
			document.getElementById("_collectReCosign").value = "";
		},
		setValue: function(value)
		{
			this.addUserRolePanel.form.setValues({
				"role_name": value.role_name,
				"userList": value.userList,
				"role_id": value.role_id
			});
			
			document.getElementById("_collectReCosign").value = value.userList;
			
			this.roleUserPanel.load(value.userList);
			
			var selectReCosign = Ext.getCmp("selectReCosign");
			if(typeof(selectReCosign)=="object"){
				selectReCosign.setReCosigners(value.userList);
				selectReCosign.load()
			}
			//alert(value.userList);
		}
	});

/****************************************************
 * @class AddUserRolePanel
 * @extends Ext.FormPanel
 ***************************************************/
	AddUserRolePanel = Ext.extend( Ext.FormPanel,{
		roleUserPanel: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			AddUserRolePanel.superclass.constructor.call(this, {
				region: 'center',
				layout: 'form',
				border: false,
				labelAlign: 'right',
				bodyStyle: 'padding:5px',
				buttonAlign: 'center',
				items: [{
				    layout: 'column',
				    border: false,
				    items:[{
				        columnWidth: 1,
				        layout: 'form',
				        border: false,
				        items: [{
				        	hidden: true,
					    	items: [{
					    		xtype: 'textfield',
					    		name: 'role_id',
						    	value: ''
					    	}]
					    },{
				        	hidden: true,
					    	items: [{
					    		xtype: 'textfield',
					    		name: 'userList',
						    	value: ''
					    	}]
					    },{
				            xtype: 'textfield',
				            fieldLabel: "职位名称",
				            maxLength: 100,
				            name: 'role_name',
				            allowBlank: false,
				            anchor: '95%'
				        }]
				    },{
				        columnWidth: 1,
				        layout: 'fit',
				        border: false,
				        items: [this.roleUserPanel]
				    }]
				}]
			});
		}
	});	

/****************************************************
 * @class RoleUserPanel
 * @extends Ext.grid.GridPanel
 ***************************************************/
AddRoleUserPanel = Ext.extend(Ext.grid.GridPanel,{
	reCosignerList: null,
	reCosignWindow: null,
	condition: null,
	cm: null,
	store: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{header:"工号", dataIndex: 'employee_ID', width:100},
			{header:"姓名", dataIndex: 'employeeName', width:120},
			{header:"英文名", dataIndex: 'employeeName_EN', width:150},
			{header:"部门", dataIndex: 'deptName', width:150}
		]);
		
		this.condition = {reCosignerList: this.reCosignerList};
		
		this.store = new Ext.data.Store({
		    baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',  
				url: '/pdm/role/getAddRoleUsers.htm'
			}),
		    reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'employee_ID'
			},[
				{name: 'employee_ID'},
		        {name: 'employeeName'},
		        {name: 'employeeName_EN'},
		        {name: 'deptName'}
			])
		});
		this.store.load();
				
		AddRoleUserPanel.superclass.constructor.call(this, {
			//id: 'addRoleUserPanel',
			region: 'center',
			border: true,
			loadMask: true,
			width: 300,
			height: 500,
			cls: 'x-rb',
			viewConfig: {forceFit: true},
			ds: this.store,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			cm: this.cm,
			tbar: ["负责人列表",'->',{
				text: "编辑",
				iconCls: 'cls_add_role',
				handler: function(){
					this.reCosignWindow = Ext.getCmp("wReCosign");
					if(!this.reCosignWindow){
						this.reCosignWindow = new ReCosignWindow({id:"wReCosign"});
					}
					this.reCosignWindow.show();
					
					var selectReCosign = Ext.getCmp("selectReCosign");
					if(typeof(selectReCosign)=="object"){
						var userList = document.getElementById("_collectReCosign").value;
						selectReCosign.setReCosigners(userList);
						selectReCosign.load()
					}
					
				}
			}]
		});
	},
	load: function(c){
		this.condition.reCosignerList = c;
		this.store.load();
	},
	reset: function(){
		this.condition.reCosignerList = "";
		this.store.load();
	}
});


/***********************************************************
 * @description override comboBox add "if (s.getCount() === 0)"
 ***********************************************************/
	Ext.override(Ext.form.ComboBox, {  
		onViewClick : function(doFocus) {  
			var index = this.view.getSelectedIndexes()[0], s = this.store, r = s.getAt(index);  
	        if (r) {  
				this.onSelect(r, index);  
	        } else if (s.getCount() === 0) {  
				this.collapse();  
	        }  
	        if (doFocus !== false) {  
				this.el.focus();  
	        }  
		}  
	});

/***********************************************************
 * @class ComboBoxMyProject
 * @extends Ext.form.ComboBox
 ***********************************************************/
	ComboBoxMyProject = Ext.extend(Ext.form.ComboBox,{
		comboBoxGrid: null,
		tpl: null,
		tplID: null,
		constructor: function(config){
			Ext.apply(this, config);
				
			if(!this.tplID)
			{
				this.tplID = Ext.id();
			}
			
			this.comboBoxGrid = new Ext.tree.TreePanel({
		        width: 178,
				height: 250,
				border: false,
				animate: true,
				enableDD: true,
				autoScroll: true,
				containerScroll: true,
				root: new Ext.tree.AsyncTreeNode({
					text: "授权项目",
					id: '0'
				}),
		        loader: new Ext.tree.TreeLoader({
					dataUrl: "/pdm/category/getMyOwnProjectTree.htm",
					clearOnLoad: false
				})
			});
			
			this.tpl = '<tpl for="."><div style="height:200px width:220px"><div id="'+this.tplID+'"></div></div></tpl>';
			
			ComboBoxMyProject.superclass.constructor.call(this, {
				store: new Ext.data.SimpleStore({fields:['value','text'],data:[['','']]}),    
				editable: false,
				mode: 'local',
				listWidth: 180,
				triggerAction: 'all',
				maxHeight: 350,
				width: 180,
				valueField: 'value',
				displayField: 'text',
				emptyText: "请选择阶段...",
				tpl: this.tpl,
				selectedClass: '',
				onSelect: Ext.emptyFn
			});
			
			this.comboBoxGrid.on('click',function(node)
			{
			    if(node.isLeaf()||node.firstChild.isLeaf())
			    {
			    	var comboBoxGrid = Ext.getCmp("combo_project_tree");
			    	var comboBoxValue = "";
			    	if(node.isLeaf())
			    	{
			    		if((!Ext.isEmpty(node.parentNode))&&(!Ext.isEmpty(node.parentNode.parentNode)))
			    		{
			    			comboBoxValue = node.parentNode.parentNode.text+" >> "+node.parentNode.text+" >> "+node.text;
			    		}
			    		else if((!Ext.isEmpty(node.parentNode)) && Ext.isEmpty(node.parentNode.parentNode))
			    		{
			    			comboBoxValue = node.parentNode.text+" >> "+node.text;
			    		}
			    		else if(Ext.isEmpty(node.parentNode))
			    		{
			    			comboBoxValue = node.text;
			    		}
			    	} 
			    	else if(node.firstChild.isLeaf())
			    	{
			    		if((!Ext.isEmpty(node.parentNode)))
			    		{
			    			comboBoxValue = node.parentNode.text+" >> "+node.text;
			    		}else{
			    			comboBoxValue = node.text;
			    		}
			    	}
			    	
				    comboBoxGrid.setValue(comboBoxValue);
				    comboBoxGrid.collapse();
				    
				    var r = node.text;
				    //alert(r);
				    //alert(r.match(/->.+->/));
				    if(r.match(/->.+->/))
				    {
				    	Ext.getCmp("btn_edit_role").disable();
				    }else{
				    	Ext.getCmp("btn_edit_role").enable();
				    }
				    
			    	var gpProjectMember = Ext.getCmp("gpMemberMaintenance");
					if(Ext.isEmpty(gpProjectMember)) Ext.Msg.alert("糟糕，系统出问题啦！");
					else gpProjectMember.reload(node.id);
			    }
			    
			});
			
			this.on('expand',function(){
				this.comboBoxGrid.render(this.tplID);   
			});
			
		}
	});